package club.xyes.zkh.ecommerce.web.api.interceptor.conf;

import club.xyes.zkh.ecommerce.security.encoder.AccessTokenEncoder;
import club.xyes.zkh.ecommerce.web.api.interceptor.AccessTokenInterceptor;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.context.annotation.Configuration;
import org.springframework.util.PathMatcher;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

/**
 * Create by 郭文梁 2019/4/19 0019 09:56
 * WebInterceptorConfiguration
 * Web拦截器相关配置
 *
 * @author 郭文梁
 * @data 2019/4/19 0019
 */
@Configuration
public class WebInterceptorConfiguration implements WebMvcConfigurer, ApplicationContextAware {
    private ApplicationContext context;
    private final AccessTokenEncoder accessTokenEncoder;

    @Autowired
    public WebInterceptorConfiguration(AccessTokenEncoder accessTokenEncoder) {
        this.accessTokenEncoder = accessTokenEncoder;
    }

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        PathMatcher matcher = context.getBean(PathMatcher.class);
        registry.addInterceptor(new AccessTokenInterceptor(matcher, accessTokenEncoder));
    }

    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        this.context = applicationContext;
    }
}
